package org.cloudie.docs.editor.client.widgets;

import org.cloudie.docs.editor.client.behaviors.HasContextMenuBehavior;
import org.cloudie.docs.editor.client.commands.Command;
import org.cloudie.docs.editor.client.content.contextmenus.IconContextMenu;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.events.DoubleClickHandler;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.Widget;

public abstract class DesktopIcon extends Icon
    implements HasContextMenuBehavior {
	
  public DesktopIcon(String id, String label, String image, String tooltip,
	    Command cmd) {
    super(id, label, image, false, tooltip, true, cmd);
    this.container.setStylePrimaryName("cloudie-desktop-icon");
    this.container.addClickHandler(new DoubleClickHandler() {
      @Override
      public void onDoubleClick(ClickEvent event) {
        if (command != null) {
          CommandEvent.fire(DesktopIcon.this.command);
        }
      }
    });
    this.container.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        container.setFocus(true);
      }
    });
    this.container.addFocusHandler(new FocusHandler() {
      public void onFocus(FocusEvent event) {
        container.addStyleDependentName("selected");
      }
    });
    this.container.addBlurHandler(new BlurHandler() {
      public void onBlur(BlurEvent event) {
        container.removeStyleDependentName("selected");
      }
    });
  }

  @Override
  public Widget getDragHandle() {
    return container;
  }
  
  @Override
  public ContextMenu getContextMenu() {
	IconContextMenu menu = IconContextMenu.get();
	menu.setTarget(this);
	return menu;
  }
  
}
